package com.mine.crawler.util;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;

public class HttpUtils
{
	private static Logger log = Logger.getLogger(HttpUtils.class);
	
	//static String ptnHttp = "http:.+?(\"|\\s)";
	static String ptnHttp = "(http|https)://[a-zA-Z0-9./&&[^\"]]*";
	static Pattern patternHttp = Pattern.compile(ptnHttp);
	static Matcher matcher;
	
	public static String getDomainFromUrl(String url)
			throws MalformedURLException
	{
		URL javaURL = new URL(url);
		
		return javaURL.getProtocol() + "://" + javaURL.getHost()
				+ (javaURL.getPort() != -1 ? ":" + javaURL.getPort() : "");
	}

	public static String getBaseUriFromUrl(String url)
			throws MalformedURLException
	{
		URL javaURL = new URL(url);
		String path = javaURL.getPath();
		int index = path.lastIndexOf("/");
		
		if (index == -1)
		{
			return "";
		}
		
		else
		{
			return path.substring(0, index);
		}
	}

	public static String extractHTTPURL(String line)
	{
		matcher = patternHttp.matcher(line);
		String result = null;
		
		while (matcher.find()) 
        {
			//result =matcher.group();
			result = (String) line.subSequence(matcher.start(), matcher.end());
			//log.info(result);
        }
		//System.out.println(result);
		return result;
	}
}
